import pandas as pd
from pyecharts.charts import *
from pyecharts import options as opts

csv = pd.read_csv('../csv/vilipixiv.csv')
counted = csv.groupby(['autor']).size()
counted.to_csv("../csv/author_count.csv", header=['work_number'])  # 存入目的地址文件

csv = pd.read_csv('../csv/author_count.csv')
counted = csv.groupby(['work_number']).size()
counted.to_csv("../csv/author.csv", header=['author_number'])

data = []
def get_pie_data(df, number, data):
    author_data = (f'上榜{df.iloc[number]["work_number"]}张作品的作者数量', df.iloc[number]["author_number"])
    # print(author_data)
    data.append(author_data)
    return author_data


def make_pie_2():
    df = pd.read_csv('../csv/author.csv', usecols=['work_number', 'author_number'])

    get_pie_data(df, 1, data)
    get_pie_data(df, 2, data)
    get_pie_data(df, 0, data)
    print(data)
    chart = Pie(
        init_opts=opts.InitOpts(),
    )
    chart.add(
        '上榜作者在榜作品数量情况',
        data_pair=[('上榜2张作品的作者数量', 14), ('上榜4张作品的作者数量', 1), ('上榜1张作品的作者数量', 234)],
    )
    chart.set_colors(['rgb(17, 0, 255)', 'rgb(255, 0, 0)', 'rgb(0,255,0)'])
    chart.set_global_opts(
        title_opts=opts.TitleOpts(
            title='上榜作者在榜作品数量情况',
            pos_left="center",
            pos_bottom="0",
            title_textstyle_opts=opts.TextStyleOpts(color="black"),
        ),
        legend_opts=opts.LegendOpts(is_show=False),
    )
    chart.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    chart.render('../html/上榜作者在榜作品数量情况饼图.html')
    return chart


if __name__ == "__main__":
    make_pie_2()
